﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using ColorSpace;
    using Controls;
    using Filter;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class SplitColorSpaceChannelsForm : Form, IOptionsForm
    {
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }
        private readonly SplitColorSpaceChannels filter;


        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="SplitColorSpaceChannelsForm"/> class.
        /// </summary>
        /// <param name="processor">The SBIP processor.</param>
        public SplitColorSpaceChannelsForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess, new SplitColorSpaceChannels 
                { ColorSpace = ColorSpaceEnum.HSB }) as SplitColorSpaceChannels;
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b = filter.Apply(Processor.OriginalImage);
            Helper.ColorPalette.SetColorPaletteToGray(b);

            string sColorChannel = rdBtnA.Checked ? "A" : 
                                  (rdBtnB.Checked ? "B" : "C");
            string s = Program.L.Text[40] + Program.L.Text[11] + ": " +
                gbColorSpace.Text + "=" + filter.ColorSpace.ToString("F") + "; " +
                gbColorChannel.Text + "=" + sColorChannel + "; ";

            Processor.Change(b, s);                        
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[11];
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57];
            gbColorSpace.Text = Program.L.Text[69];
            gbColorChannel.Text = Program.L.Text[70];

        }

        #region Tab control events

        private void ComBoxColorSpaceSelectedIndexChanged(object sender, EventArgs e)
        {
            filter.ColorSpace = (ColorSpaceEnum)((ComboBox)sender).SelectedIndex;
        }

        private void RdBtnACheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                filter.ColorTriple = ColorTripleEnum.A;
        }

        private void RdBtnBCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                filter.ColorTriple = ColorTripleEnum.B;
        }

        private void RdBtnCCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                filter.ColorTriple = ColorTripleEnum.C;
        }

        #endregion Tab control events

        private void BtnProcessClick(object sender, EventArgs e)
        {
            EndlessProgressBarFormInThread form =
                new EndlessProgressBarFormInThread(
                    Program.L.Text[63],
                    Program.L.Text[64]);
            form.Start();

            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }
    }
}
